<template>
  <div class="chapter-practice">
    <white-header
      :isShowTopNav="true"
      :isShowBack="true"
      :isShowRight="true"
      :title="title"
    ></white-header>
    <div class="progress">
      <!-- <van-icon class="delete-icon-all" name="delete" size="30" @click="deleteChapterRecord"/> -->
      <span class="delete-btn-all" @click="deleteChapterRecord">清空</span>
      <div class="progress-box progress-left">
        <van-circle
          v-model="studyStatus.leftRatio"
          :rate="studyStatus.leftRatio"
          :speed="100"
          layer-color="rgba(255, 255, 255, 0.2)"
          color="#fff"
          :text="''"
          stroke-width="60"
          size="68%"
        />
        <div class="main-box">
          <img src="/img/mobile/speed-dash.png" class="dash-line" />
        </div>
        <div class="content-box">
          <div class="rate">
            {{ studyStatus.doneNum }}/{{ studyStatus.questionNum }}
          </div>
          <div class="text">学习进度</div>
        </div>
      </div>
      <div class="progress-box progress-right">
        <van-circle
          v-model="studyStatus.rightRatio"
          :rate="studyStatus.rightRatio"
          :speed="100"
          layer-color="rgba(255, 255, 255, 0.2)"
          color="#fff"
          :text="''"
          stroke-width="60"
          size="140px"
        />
        <div class="main-box">
          <img src="/img/mobile/speed-dash.png" class="dash-line" />
        </div>
        <div class="content-box">
          <div class="rate">{{ studyStatus.accuracy }}%</div>
          <div class="text">正确率</div>
        </div>
      </div>
    </div>
    <div class="practice" v-if="lastChapter.chapterId">
      <div class="practice-left">
        <img src="/img/mobile/practice_continue.png" alt="" />
      </div>
      <div class="practice-center">
        {{ lastChapter.chapterName || "未知" }}
      </div>
      <div class="practice-right" @click="toChapter">
        <div class="practice-btn">继续做题</div>
      </div>
    </div>
    <div class="chapter-list">
      <div
        class="chapter-box"
        v-for="(item, index) in chapterTree"
        :key="'chapterTree' + index"
        @click="toPath(item)"
      >
        <div class="tags" v-if="item.testQuestionNum == item.doneQuestionNum">
          已完成
        </div>
        <div class="chapter-left">
          <div class="title">
            {{ item && item.chapterName }}
          </div>
          <div class="chapter-info">
            <div class="answer">
              <img src="/img/mobile/record_answer.png" />
              <p class="answer-count">
                已答题：<span style="color:#ed8585;"
                  >{{ (item && item.doneQuestionNum) || 0 }}/{{
                    item && item.testQuestionNum
                  }}道</span
                >
              </p>
            </div>
            <div class="correct">
              <img src="/img/mobile/record_time.png" alt="" />
              <p class="correct-count">
                正确率：<span>{{ (item && item.accuracy) || 0 }}%</span>
              </p>
            </div>
          </div>
        </div>
        <div
          class="chapter-right"
          v-if="item.testQuestionNum == item.doneQuestionNum"
        >
          <span style="color:#ed8585;">回顾</span>
        </div>
        <div class="chapter-right" v-else>
          <img src="/img/mobile/right-icon.png" alt="" />
        </div>
      </div>
      <van-empty
        v-if="chapterTree && chapterTree.length == 0"
        :description="'当前题库无章节哦~'"
      />
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import {
  getQuestionBankChapterTree,
  getChapterSchedule,
  getQuestionBankChapterList,
  getChapterLast,
  deleteAllChapterRecord
} from "~/api/v2-question-bank/question-bank.js";
export default {
  components: {
    WhiteHeader
  },
  data() {
    return {
      title: "章节练习",
      /* 题库章节树 */
      chapterTree: [],
      /* 章节列表 */
      chapterList: [],
      /* 进度和正确率 */
      studyStatus: {
        accuracy: 0,
        doneNum: 0,
        questionNum: 0,
        leftRatio: 0,
        rightRatio: 0,
        trueCount:0,
      },
      /* 上次做到的章节 */
      lastChapter: {
        chapterId: "",
        chapterName: "",
        createTime: 0,
        id: 0,
        lastIndex: 0,
        userId: 0
      }
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      is_loading(state) {
        return state.is_loading;
      }
    }),
    questionId() {
      return this.$route.query.id;
    }
  },
  watch: {},
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    /* 删除全部章节做题记录 */
    deleteChapterRecord(){
      console.log("111");
      this.$dialog.confirm({
            title: '提示',
            message: '是否清空所有章节练习的记录？',
          })
          .then(async () => {
            let params = {
              questionGroupId: this.questionId,
              userId: this.user_info.id
            }
            let result = await deleteAllChapterRecord(params);
            if (result && result.data && result.data.code == 200) {
              this.$toast("清空成功!");
              this.initPage();
            } else {
              this.$toast("清空失败!请稍后重试");
            }
            
          })
          .catch(() => {
          });
    },
    /* 获取章节列表 */
    async getChapterList() {
      this.SET_IS_LOADING(true);
      let params = {
        questionGroupId: this.questionId,
        userId: this.user_info.id
      };
      let result = await getQuestionBankChapterList(params);
      console.log("获取章节列表:", result);
      if (result && result.data && result.data.code == 200) {
        const data = result.data.data;
        if (data) {
          this.chapterTree = data;
        }
      } else {
        this.$toast("获取失败!请稍后重试");
      }
      this.SET_IS_LOADING(false);
    },
    /* 获取章节树 */
    async getChapterTree() {
      this.SET_IS_LOADING(true);
      let params = {
        questionGroupId: this.questionId,
        userId: this.user_info.id
      };
      let result = await getQuestionBankChapterTree(params);
      console.log("result:", result);
      if (result && result.data && result.data.code == 200) {
        const { data } = result.data;
        this.chapterTree = data;
      } else {
        this.$toast("获取失败!请稍后重试");
      }
      this.SET_IS_LOADING(false);
    },
    /* 获取章节进度和正确率 */
    async getChapterSchedule() {
      this.SET_IS_LOADING(true);
      let params = {
        questionGroupId: this.questionId,
        userId: this.user_info.id
      };
      let result = await getChapterSchedule(params);
      console.log("章节进度和正确率:", result);
      if (result && result.data && result.data.code == 200) {
        const { data } = result.data;
        const { accuracy, doneNum, questionNum,trueCount } = data;
        this.studyStatus = {
          accuracy: Number(accuracy),
          trueCount: Number(trueCount),
          doneNum: Number(doneNum),
          questionNum: Number(questionNum),
          rightRatio:(Number(trueCount) / Number(questionNum)) * 100,
          leftRatio: (Number(doneNum) / Number(questionNum)) * 100
        };
      } else {
        this.$toast("获取失败!请稍后重试");
      }
      this.SET_IS_LOADING(false);
    },
    /* 获取上次做到的章节 */
    async getChapterLast() {
      this.SET_IS_LOADING(true);
      let result = await getChapterLast({
        userId: this.user_info.id,
        questionGroupId: this.questionId
      });
      console.log("获取上次做到的章节：", result);
      if (result && result.data && result.data.code == 200) {
        const { data } = result.data;
        if (data) {
          this.lastChapter = data;
        }
      }
      this.SET_IS_LOADING(false);
    },
    toPath(detail) {
      if (detail.node.length > 0) {
        this.$router.push({
          path:
            "/m/chapter-practice-inner?id=" +
            this.questionId +
            "&chapterId=" +
            detail.id
        });
      } else {
        this.$router.push({
          path:
            "/m/v2-answer-sheet?id=" +
            this.questionId +
            "&type=0&chapterId=" +
            detail.id
        });
      }
    },
    toChapter() {
      if (this.lastChapter && this.lastChapter.chapterId) {
        this.$router.push({
          path:
            "/m/v2-answer-sheet?id=" +
            this.questionId +
            "&type=0&chapterId=" +
            this.lastChapter.chapterId
        });
      }
    },
    initPage() {
      this.getChapterList();
      // this.getChapterTree();
      this.getChapterSchedule();
      this.getChapterLast();
    }
  },
  created() {
    this.initPage();
  },
  activated() {
    this.initPage();
  },
  mounted() {}
};
</script>

<style lang="scss">
.chapter-practice {
  background-color: #f7f7fa;
  min-height: 100vh;
  width: 7.2rem;
  .progress {
    display: flex;
    height: 3.936rem;
    background-image: linear-gradient(
      145deg,
      var(--theme-color),
      var(--theme-color-lighter)
    );
    position: relative;
    .delete-icon-all{
      position: absolute;
      right: .1rem;
      top: .1rem;
      color: #ffffff;
      z-index: 999;
    }
    .delete-btn-all{
      font-size: .3rem;
      position: absolute;
      right: .1rem;
      top: .1rem;
      color: #ffffff;
      z-index: 999;
    }
    .progress-box {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      .main-box {
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        .dash-line {
          transform: translateY(-30%);
          width: 110px;
          height: auto;
        }
      }
      .content-box {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        text-align: center;
        .rate {
          margin-top: 50%;
          margin-bottom: 0.336rem;
          font-family: DIN-Regular;
          font-size: 0.365rem;
          font-stretch: normal;
          line-height: 0.149rem;
          letter-spacing: 0rem;
          color: #ffffff;
          //   font-weight: bold;
        }
        .text {
          font-family: KozGoPr6N-Regular;
          font-size: 0.242rem;
          letter-spacing: 0rem;
          color: #ffffff;
          font-weight: bold;
        }
      }
    }
  }
  .practice {
    width: 7.2rem;
    height: 1.4976rem;
    background: #ffffff;
    display: flex;
    align-items: center;
    .practice-left {
      width: 0.96rem;
      img {
        display: block;
        width: 0.3072rem;
        height: 0.3072rem;
        margin: 0 auto;
        background: var(--theme-color);
      }
    }
    .practice-center {
      width: 4.32rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      color: #4a4b4d;
      font-weight: 0.288rem;
      font-weight: bold;
    }
    .practice-right {
      width: 1.92rem;
      .practice-btn {
        color: #ffffff;
        text-align: center;
        line-height: 0.5376rem;
        width: 1.44rem;
        height: 0.5376rem;
        background: var(--theme-color);
        border-radius: 0.2688rem;
        font-size: 0.2304rem;
        margin: 0 auto;
      }
    }
  }
  .chapter-list {
    margin-top: 0.192rem;
    background: #ffffff;
    .chapter-box {
      width: 7.2rem;
      height: 1.7856rem;
      border-bottom: 1px solid #ededf1;
      padding: 0.46rem 0.3rem;
      display: flex;
      position: relative;
      overflow: hidden;
      .tags {
        position: absolute;
        left: -0.34559rem;
        top: 0.1152rem;
        width: 1.2rem;
        height: 0.3rem;
        background: var(--theme-color);
        text-align: center;
        line-height: 0.3rem;
        color: #ffffff;
        font-size: 12px;
        transform: rotateZ(-45deg) scale(0.8);
      }
      &:last-child {
        border: none;
      }
      .chapter-left {
        width: 90%;
        .title {
          width: 100%;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          color: #4a4b4d;
          font-weight: 0.288rem;
          font-weight: bold;
          margin-bottom: 0.2rem;
        }
        .chapter-info {
          width: 100%;
          display: flex;
          .answer {
            width: 50%;
            display: flex;
            align-items: center;
            img {
              width: 0.288rem;
              height: 0.288rem;
              display: block;
              background: var(--theme-color);
              margin-right: 0.1rem;
            }
            .answer-count {
              font-size: 0.2304rem;
              color: #949699;
            }
          }
          .correct {
            width: 50%;
            display: flex;
            align-items: center;
            img {
              width: 0.288rem;
              height: 0.288rem;
              display: block;
              background: var(--theme-color);
              margin-right: 0.1rem;
            }
            .correct-count {
              font-size: 0.2304rem;
              color: #949699;
              span {
                color: var(--theme-color);
              }
            }
          }
        }
      }
      .chapter-right {
        width: 10%;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        img {
          display: block;
          width: 0.2304rem;
          height: 0.2304rem;
        }
      }
    }
  }
}
</style>
